//
//  GameOverView.swift
//  1010
//
//  Created by 米明 on 15/7/23.
//  Copyright (c) 2015年 米明. All rights reserved.
//

import UIKit

let MENU_WIDTH = CGFloat(280)
let SPACE = CGFloat(2)
let FONTNAME = "STHeitiK-Light"
let FONT_STANK = "Stank"
let FONT_HEIBAN = "Chalkboard SE"
let FONT_TATULA = "TATU"
let FONTNAME_CN = "Heiti SC"

//开始菜单 结束菜单

@available(iOS 8.0, *)
class MenuView: UIView {
    
    var bg : UIVisualEffectView!
    var score : UIButton!
    var home : UIButton!
    var restart : UIButton!
    var share : UIButton!
    
    //按钮触发回调
    var scoreBack:CallBack!
    var startBack:CallBack!
    var settingBack:CallBack!
    var shareBack:CallBack!

    var isShow : Bool = false
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUp()
    }
    
    func setUp() {
        configMenu()
    }
    
    
    func show(delay:Double) {
        //背景
        UIView.animate(withDuration: 0.5, delay: delay, options: [], animations: {
            self.bg.alpha = 0.5
        }, completion: nil)
        
        //score
        UIView.animate(withDuration: 0.5, delay: delay+0.1, usingSpringWithDamping: 0.6, initialSpringVelocity: 2.0, options: [], animations: { () -> Void in
            self.score.transform=CGAffineTransform(scaleX: 1, y: 1)
            self.score.center.y -= self.score.frame.size.height/2.0
        }) { (finished) -> Void in
            self.score.frame.size.width=self.score.frame.size.width+2*SPACE
            self.score.center.x -= SPACE
        }
        
        //home
        UIView.animate(withDuration: 0.5, delay: delay+0.2, usingSpringWithDamping: 0.6, initialSpringVelocity: 2.0, options: [], animations: { () -> Void in
            self.home.transform=CGAffineTransform(scaleX: 1, y: 1)
            self.home.center.y += self.home.frame.size.height/2.0+SPACE

            self.home.center.x -= self.home.frame.size.width+SPACE

            }) { (finished) -> Void in
        }
        
        //restart
        UIView.animate(withDuration: 0.5, delay: delay+0.3, usingSpringWithDamping: 0.6, initialSpringVelocity: 2.0, options: [], animations: { () -> Void in
            self.restart.transform=CGAffineTransform(scaleX: 1, y: 1)
            self.restart.center.y += self.restart.frame.size.height/2.0+SPACE

            }) { (finished) -> Void in
        }
        
        //share
        UIView.animate(withDuration: 0.5, delay: delay+0.4, usingSpringWithDamping: 0.6, initialSpringVelocity: 2.0, options: [], animations: { () -> Void in
            self.share.transform=CGAffineTransform(scaleX: 1, y: 1)
            self.share.center.y += self.share.frame.size.height/2.0+SPACE

            self.share.center.x += self.share.frame.size.width+SPACE

            }) { (finished) -> Void in
        }
        
    }
    
    func dismiss() {
        UIView.animate(withDuration: 0.2, animations: {
            self.alpha=0
        }) { (finished) -> Void in
            self.removeFromSuperview()
            self.isShow = false
        }
    }
    
    func configMenu() {
        setUpBg()
        setUpScore()
        setUpHome()
        setUpRestart()
        setUpShare()
        self.isShow = true
    }
    
    func setUpBg() {
        bg=UIVisualEffectView(effect: UIBlurEffect(style: .light))
        bg?.frame=UIScreen.main.bounds
        bg.alpha=0.1
        self.addSubview(bg)
    }

    
    //MARK: 四个按钮
    func setUpScore() {
        score = produceButton(frame: CGRect(x: 0, y: 0, width: MENU_WIDTH, height: MENU_WIDTH/2.0))
        score.backgroundColor=customColor(red: 176, green: 221, blue: 108, alpha: 1)
        score.addTarget(self, action: #selector(scoreAction(_:)), for: UIControl.Event.touchUpInside)
    }
    
    func setUpHome() {
        home = produceButton(frame: CGRect(x: 0, y: 0, width: MENU_WIDTH/3.0, height: MENU_WIDTH/3.0))
        home.backgroundColor=customColor(red: 236, green: 102, blue: 58, alpha: 1)
        home.addTarget(self, action: #selector(homeAction(_:)), for: UIControl.Event.touchUpInside)
    }
    
    func setUpRestart() {
        restart = produceButton(frame: CGRect(x: 0, y: 0, width: MENU_WIDTH/3.0, height: MENU_WIDTH/3.0))
        restart.backgroundColor=customColor(red: 190, green: 135, blue: 217, alpha: 1)
        restart.addTarget(self, action: #selector(restartAction(_:)), for: UIControl.Event.touchUpInside)
    }
    
    func setUpShare() {
        share = produceButton(frame: CGRect(x: 0, y: 0, width: MENU_WIDTH/3.0, height: MENU_WIDTH/3.0))
        share.backgroundColor=customColor(red: 92, green: 152, blue: 114, alpha: 1)
        share.addTarget(self, action: #selector(shareAction(_:)), for: UIControl.Event.touchUpInside)
    }
    
    //MARK: 传递函数
    
    func scoreCallBack(callBack:@escaping ()->()) {
        scoreBack = callBack
    }
    
    func settingCallBack(callBack:@escaping ()->()) {
        settingBack = callBack
    }
    
    func startCallBack(callBack:@escaping ()->()) {
        startBack = callBack
    }
    
    func shareCallBack(callBack:@escaping ()->()) {
        shareBack = callBack
    }
    
    //MARK: 按钮触发
    @objc func scoreAction(_ btn:UIButton) {
        scoreBack()
    }
    
    @objc func homeAction(_ btn:UIButton) {
        settingBack()
    }
    
    @objc func restartAction(_ btn:UIButton) {
        startBack()
    }
    

    @objc func shareAction(_ btn:UIButton) {
        shareBack()
    }
    
    //MARK: 生产button
    func produceButton(frame: CGRect) -> UIButton! {
        let btn = UIButton(type: .system)
        btn.frame=frame
        btn.center=CGPoint(x: SCREEN_SIZE.width/2.0, y: SCREEN_SIZE.height/2.0)
        btn.transform=CGAffineTransform(scaleX: 0, y: 0)
        btn.layer.masksToBounds=true
        btn.layer.cornerRadius=SPACE
        btn.tintColor=UIColor.white
        self.addSubview(btn)
        return btn
    }
    
    //MARK: 主页菜单
    func showHomeMenu() {
        bg.backgroundColor=UIColor.clear
        //标志
        self.score.setImage(UIImage(named: "c"), for: UIControl.State.normal)
        //设置
        self.home.setImage(UIImage(named: "set"), for: UIControl.State.normal)
        //开始
        self.restart.setImage(UIImage(named: "play"), for: UIControl.State.normal)
        //分享
        self.share.setImage(UIImage(named: "star"), for: UIControl.State.normal)
        
        show(delay: 0)
    }
    
    func dismissHomeMenu() {
        self.score.center = CGPoint(x: SCREEN_SIZE.width/2.0, y: SCREEN_SIZE.height/2.0)
        self.score.transform = CGAffineTransform(scaleX: 0, y: 0)
        self.home.center = CGPoint(x: SCREEN_SIZE.width/2.0, y: SCREEN_SIZE.height/2.0)
        self.home.transform = CGAffineTransform(scaleX: 0, y: 0)
        self.restart.center = CGPoint(x: SCREEN_SIZE.width/2.0, y: SCREEN_SIZE.height/2.0)
        self.restart.transform = CGAffineTransform(scaleX: 0, y: 0)
        self.share.center = CGPoint(x: SCREEN_SIZE.width/2.0, y: SCREEN_SIZE.height/2.0)
        self.share.transform = CGAffineTransform(scaleX: 0, y: 0)
    }
    
    //MARK: gameOver菜单
    func showEndMenu() {
        //分数
        self.score.setImage(UIImage(named: "c"), for: UIControl.State.normal)
        
        self.score.titleLabel?.font=UIFont(name: FONT_STANK, size: 58)
        //home
        self.home.setImage(UIImage(named: "home"), for: UIControl.State.normal)
        //重来
        self.restart.setImage(UIImage(named: "repeat"), for: UIControl.State.normal)
        //分享
        self.share.setImage(UIImage(named: "share"), for: UIControl.State.normal)
        
        UIView.animate(withDuration: 0.6, animations: {
            self.score.alpha = self.score.alpha
        }) { (finished) -> Void in
            
            self.score.setTitle("\(DataCenter.nowScore())", for: UIControl.State.normal)
            
        }
        
        show(delay: Double(0))
    }
    
    //MARK: pause菜单
    func showPauseMenu() {
        //标志
        self.score.setImage(UIImage(named: "c"), for: UIControl.State.normal)
        //home
        self.home.setImage(UIImage(named: "home"), for: UIControl.State.normal)
        //继续
        self.restart.setImage(UIImage(named: "play"), for: UIControl.State.normal)
        //重来
        self.share.setImage(UIImage(named: "repeat"), for: UIControl.State.normal)
        
        show(delay: 0)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}
